package com.jie.deletefile.DIYjindutiao;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.jie.deletefile.R;

import java.util.Timer;
import java.util.TimerTask;



/**
 * Created by Justin on 2016/5/19.
 */
public class PiechartView extends View {

    //画笔
    private Paint paint;
    //画圆形的矩形区域
    private RectF oval;

    //三种颜色
    private int fristColor;
    private int secondColor;
    private int baseColor;


    private float proportionPhone = 0;
    private float proportionSD = 0;
    private float firstAngle = 0;
    private float secondAngle = 0;




    //在XML文件中注册，用两个参数的构造器
    public PiechartView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint = new Paint();
        fristColor = getResources().getColor(R.color.piechar_phone);
        secondColor = getResources().getColor(R.color.piechar_sdcard);
        baseColor = getResources().getColor(R.color.piechar_base);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        int viewHeight = MeasureSpec.getSize(heightMeasureSpec);

        oval = new RectF(0,0,viewWidth,viewHeight);

        setMeasuredDimension(viewWidth,viewHeight);

    }

    //设置第一种颜色
    public void setSecondColor(int secondColor) {
        this.secondColor = secondColor;
    }

    //设置底色
    public void setBaseColor(int baseColor) {
        this.baseColor = baseColor;
    }

    //设置地二中颜色
    public void setFristColor(int fristColor) {
        this.fristColor = fristColor;
    }

    /**
     * 两个进度的比例,参数小于等于1
     * @param f1
     * @param f2
     */
    public void setPiechartProportionWithAnim(float f1, float f2){
        proportionPhone = f1;
        proportionSD = f2;

        final float phoneTargetAngle = 360 * proportionPhone;
        final float sdTargetAngle = 360 * proportionSD;

        final Timer timer = new Timer();
        TimerTask timeerTask = new TimerTask() {
            @Override
            public void run() {
                firstAngle += 4;
                secondAngle += 4;
                postInvalidate();
                if(firstAngle >=phoneTargetAngle){
                    firstAngle = phoneTargetAngle;
                }

                if(secondAngle >=sdTargetAngle){
                    secondAngle = sdTargetAngle;
                }

                if(firstAngle == phoneTargetAngle && secondAngle == sdTargetAngle){
                    timer.cancel();
                }

            }
        };
        timer.schedule(timeerTask,26,26);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(baseColor);
        canvas.drawArc(oval,-90,360,true,paint);

        if (secondAngle > firstAngle) {

            paint.setColor(secondColor);
            canvas.drawArc(oval, -90, secondAngle, true, paint);
            paint.setColor(fristColor);
            canvas.drawArc(oval, -90, firstAngle, true, paint);
        } else {
            paint.setColor(fristColor);
            canvas.drawArc(oval,-90, firstAngle,true,paint);

            paint.setColor(secondColor);
            canvas.drawArc(oval,-90, secondAngle,true,paint);
        }




    }
}
